﻿using System;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace Terraria.Localization
{
	// Token: 0x02000324 RID: 804
	public class LocalizedText
	{
		// Token: 0x17000216 RID: 534
		// (get) Token: 0x06001E30 RID: 7728 RVA: 0x000156B1 File Offset: 0x000138B1
		// (set) Token: 0x06001E31 RID: 7729 RVA: 0x000156CD File Offset: 0x000138CD
		public string Value
		{
			get
			{
				if (this.Override != null)
				{
					return this.Override.Value;
				}
				return this.value;
			}
			private set
			{
				this.value = value;
			}
		}

		// Token: 0x06001E32 RID: 7730 RVA: 0x000156D6 File Offset: 0x000138D6
		internal LocalizedText(string key, string text)
		{
			this.Key = key;
			this.Value = text;
		}

		// Token: 0x06001E33 RID: 7731 RVA: 0x000156EC File Offset: 0x000138EC
		internal void SetValue(string text)
		{
			this.Value = text;
		}

		// Token: 0x06001E34 RID: 7732 RVA: 0x004415D4 File Offset: 0x0043F7D4
		public string FormatWith(object obj)
		{
			string input = this.Value;
			PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
			return LocalizedText._substitutionRegex.Replace(input, delegate(Match match)
			{
				if (match.Groups[1].Length != 0)
				{
					return "";
				}
				string name = match.Groups[2].ToString();
				PropertyDescriptor propertyDescriptor = properties.Find(name, false);
				if (propertyDescriptor == null)
				{
					return "";
				}
				return (propertyDescriptor.GetValue(obj) ?? "").ToString();
			});
		}

		// Token: 0x06001E35 RID: 7733 RVA: 0x00441620 File Offset: 0x0043F820
		public bool CanFormatWith(object obj)
		{
			PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
			MatchCollection matchCollection = LocalizedText._substitutionRegex.Matches(this.Value);
			foreach (object obj2 in matchCollection)
			{
				Match match = (Match)obj2;
				string name = match.Groups[2].ToString();
				PropertyDescriptor propertyDescriptor = properties.Find(name, false);
				if (propertyDescriptor == null)
				{
					return false;
				}
				object obj3 = propertyDescriptor.GetValue(obj);
				if (obj3 == null)
				{
					return false;
				}
				if (match.Groups[1].Length != 0 && (((obj3 as bool?) ?? false) ^ match.Groups[1].Length == 1))
				{
					return false;
				}
			}
			return true;
		}

		// Token: 0x06001E36 RID: 7734 RVA: 0x000156F5 File Offset: 0x000138F5
		public NetworkText ToNetworkText()
		{
			return NetworkText.FromKey(this.Key, new object[0]);
		}

		// Token: 0x06001E37 RID: 7735 RVA: 0x00015708 File Offset: 0x00013908
		public NetworkText ToNetworkText(params object[] substitutions)
		{
			return NetworkText.FromKey(this.Key, substitutions);
		}

		// Token: 0x06001E38 RID: 7736 RVA: 0x00015716 File Offset: 0x00013916
		public static explicit operator string(LocalizedText text)
		{
			return text.Value;
		}

		// Token: 0x06001E39 RID: 7737 RVA: 0x0001571E File Offset: 0x0001391E
		public string Format(object arg0)
		{
			return string.Format(this.Value, arg0);
		}

		// Token: 0x06001E3A RID: 7738 RVA: 0x0001572C File Offset: 0x0001392C
		public string Format(object arg0, object arg1)
		{
			return string.Format(this.Value, arg0, arg1);
		}

		// Token: 0x06001E3B RID: 7739 RVA: 0x0001573B File Offset: 0x0001393B
		public string Format(object arg0, object arg1, object arg2)
		{
			return string.Format(this.Value, arg0, arg1, arg2);
		}

		// Token: 0x06001E3C RID: 7740 RVA: 0x0001574B File Offset: 0x0001394B
		public string Format(params object[] args)
		{
			return string.Format(this.Value, args);
		}

		// Token: 0x06001E3D RID: 7741 RVA: 0x00015759 File Offset: 0x00013959
		public override string ToString()
		{
			return this.Value;
		}

		// Token: 0x0400194A RID: 6474
		public static readonly LocalizedText Empty = new LocalizedText("", "");

		// Token: 0x0400194B RID: 6475
		private static Regex _substitutionRegex = new Regex("{(\\?(?:!)?)?([a-zA-Z][\\w\\.]*)}", RegexOptions.Compiled);

		// Token: 0x0400194C RID: 6476
		public readonly string Key;

		// Token: 0x0400194D RID: 6477
		public LocalizedText Override;

		// Token: 0x0400194E RID: 6478
		private string value;
	}
}
